package com.tbit.uqbike.client.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.tbit.uqbike.client.dao.core.ParkMachineDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Machine;
import com.tbit.uqbike.client.pojo.ParkMachine;
import com.tbit.uqbike.client.service.MachineService;
import com.tbit.uqbike.client.service.ParkMachineService;
import com.tbit.uqbike.client.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("parkMachineService")
public class ParkMachineServiceImpl implements ParkMachineService {
    @Autowired
    private ParkMachineDao parkMachineDao;
    @Autowired
    private MachineService machineService;

    private static final Integer INSIDE = 1;

    @Override
    public void insert(ParkMachine parkMachine) {
        ParkMachine parkMachineDB = getByMachineId(parkMachine.getMachineId());
        if (parkMachineDB != null) {
            parkMachineDao.edit(parkMachine);
        } else {
            parkMachineDao.insert(parkMachine);
        }

    }

    @Override
    public void del(Integer machineId) {
        parkMachineDao.del(machineId);
    }

    @Override
    public List<ParkMachine> getByParkPointId(Integer parkPointId) {
        return parkMachineDao.getByParkPointId(parkPointId);
    }

    @Override
    public ParkMachine getByMachineId(Integer machineId) {
        return parkMachineDao.getByMachineId(machineId);
    }

    @Override
    public List<ParkMachine> getByMachineNOs(List<String> machineNOs) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineNOs", StringUtil.getArrayListString(machineNOs));

        return parkMachineDao.getByMachineNOs(params);
    }

    @Override
    public Integer getParkPointCanBorrowNum(Integer parkPointId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("parkPointId", parkPointId);

        return parkMachineDao.getParkPointCanBorrowNum(params);
    }

    @Override
    public List<Map<String, Object>> getParkPointCanBorrowNums(List<Integer> parkPointIds) {
        return parkMachineDao.getParkPointCanBorrowNums(parkPointIds);
    }

    @Override
    public Integer getParkPointCount(Integer parkPointId) {
        return parkMachineDao.getParkPointCount(parkPointId);
    }

    @Override
    public void edit(ParkMachine parkMachine) {
        parkMachineDao.edit(parkMachine);
    }

    @Override
    public Integer checkIfMachineInPark(Integer accountId, String userCode) {
        Machine machine = Optional.ofNullable(machineService.getByUserCode(userCode))
                                  .orElseThrow(() -> new BaseException("设备不存在"));
        ParkMachine p= parkMachineDao.getByMachineId(machine.getMachineId());
        return Optional.ofNullable(p)
                       .filter(parkMachine -> INSIDE.equals(parkMachine.getInside()))
                       .map(ParkMachine::getParkPointId)
                       .orElse(null);

    }


}
